package com.styytech.dadabao.ui.more;

import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.PopupWindow.OnDismissListener;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.loopj.android.http.RequestParams;
import com.styytech.dadabao.R;
import com.styytech.dadabao.api.requestresult.CommonResult;
import com.styytech.dadabao.api.response.AbstractResponseResult;
import com.styytech.dadabao.api.templates.HttpRequest;
import com.styytech.dadabao.base.BaseFragment;
import com.styytech.dadabao.constant.ConstantsServerUrl;
import com.styytech.dadabao.exception.ArgumentsException;
import com.styytech.dadabao.exception.RequestException;
import com.styytech.dadabao.module.share.SharedDialogActivity;
import com.styytech.dadabao.utils.CommonUtils;
import com.styytech.dadabao.utils.SpUser;

/**
 * Created by user on 2015/8/24.
 */
public class MoreFragment extends BaseFragment implements OnClickListener, OnDismissListener {
    private String phoneNum = "4008618778";
    private View rootView;
    //服务公告
    @ViewInject(R.id.rlyt_fwgg)
    private RelativeLayout rlyt_fwgg;
    //帮助中心
    @ViewInject(R.id.rlyt_bzzx)
    private RelativeLayout rlyt_bzzx;
    //关于我们
    @ViewInject(R.id.rlyt_gywm)
    private RelativeLayout rlyt_gywm;
    //应用评分
    @ViewInject(R.id.rlyt_yypf)
    private RelativeLayout rlyt_yypf;
    //意见反馈
    @ViewInject(R.id.rlyt_yjfk)
    private RelativeLayout rlyt_yjfk;
    //转给朋友
    @ViewInject(R.id.rlyt_zgpy)
    private RelativeLayout rlyt_zgpy;
    //推送消息开关
    @ViewInject(R.id.iv_switch)
    private ImageView iv_switch;
    //退出登录
    @ViewInject(R.id.btn_close_login)
    private Button btn_close_login;
    //客服电话
    @ViewInject(R.id.rlyt_kfdh)
    private RelativeLayout rlyt_kfdh;
    private Boolean isOpen;

    private LinearLayout myView;
    private PopupWindow myWindow;
    @ViewInject(R.id.set_menu_background)
    private RelativeLayout pop_menu_background;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        rootView = super.onCreateView(inflater, container, savedInstanceState,
                R.layout.fragment_more);
        return rootView;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initViews();
    }

    @Override
    public void onResume() {
        super.onResume();
        initViews();
        if (SpUser.getUserToken(getmActivity()) == null || SpUser.getUserToken(getmActivity()).equals("")) {
            btn_close_login.setVisibility(View.GONE);
        } else {
            btn_close_login.setVisibility(View.VISIBLE);
        }
    }

    private void initViews() {
        isOpen = SpUser.getSwitchOpen(getmActivity());
        if (isOpen) {
            iv_switch.setBackgroundResource(R.drawable.switch_open);
        } else {
            iv_switch.setBackgroundResource(R.drawable.switch_close);
        }
    }

    @OnClick({R.id.rlyt_fwgg, R.id.rlyt_bzzx, R.id.rlyt_gywm, R.id.rlyt_yypf,
            R.id.rlyt_yjfk, R.id.rlyt_zgpy, R.id.iv_switch, R.id.btn_close_login, R.id.rlyt_kfdh})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rlyt_fwgg:
                //服务公告
                CommonUtils.startActivity(context, NoticeFragmentActivity.class);
                break;
            case R.id.rlyt_bzzx:
                //帮助中心
                CommonUtils.startActivity(context, HelpCenterActivity.class);
                break;
            case R.id.rlyt_gywm:
                //关于我们
                CommonUtils.startActivity(context, AboutUsActivity.class);
                break;
            case R.id.rlyt_kfdh:
                //客服电话
                Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phoneNum));
                startActivity(intent);
                break;
            case R.id.rlyt_yypf:
                //应用评分
                try {
                    Uri uri = Uri.parse("market://details?id=" + getActivity().getPackageName());
                    Intent i = new Intent(Intent.ACTION_VIEW, uri);
                    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(i);
                } catch (ActivityNotFoundException e) {
                    Toast.makeText(getActivity(), "您的手机还没有安装任何安卓应用市场.",
                            Toast.LENGTH_SHORT).show();
                    e.printStackTrace();
                }
                break;
            case R.id.rlyt_yjfk:
                //意见反馈
                startActivity(new Intent(getmActivity(), FeedBackActivity.class));
                break;
            case R.id.rlyt_zgpy:
                //转给朋友
                Intent intentshare = new Intent(getmActivity(), SharedDialogActivity.class);

                intentshare.putExtra("title", "大大宝");
                intentshare.putExtra("content", "我正在使用大大宝手机客户端，你也快来下载吧！" + "  http://www.dadabao.net:8380/dadabaoApp/apptool/app/appdownview/down/appDown?businessAppType=6");
                intentshare.putExtra("newsPic", "http://a3.qpic.cn/psb?/V13WHAXa1pm8zp/rfMYjf08AXruAXPbJQsBhiZ2WkPkKhS4IP0f2QIeJJQ!/b/dHYBAAAAAAAA&bo=kACQAAAAAAADByI!&rf=viewer_4");
                intentshare.putExtra("shareUrl", "http://www.dadabao.net:8380/dadabaoApp/apptool/app/appdownview/down/appDown?businessAppType=6");

                startActivity(intentshare);
                break;
            case R.id.iv_switch:
                //消息推送开关
                if (isOpen) {
                    isOpen = false;
                    iv_switch.setBackgroundResource(R.drawable.switch_close);
                    Toast.makeText(getActivity(),
                            "消息推送关闭",
                            Toast.LENGTH_SHORT).show();
                } else {
                    isOpen = true;
                    iv_switch.setBackgroundResource(R.drawable.switch_open);
                    Toast.makeText(getActivity(),
                            "消息推送开启",
                            Toast.LENGTH_SHORT).show();
                }
                SpUser.setSwitchOpen(getmActivity(), isOpen);
                break;
            case R.id.btn_close_login:
                shotSelectImages();
                break;
            case R.id.btn_cancle:
                myWindow.dismiss();
                break;
            case R.id.btn_login_out:
                myWindow.dismiss();
                loginout();
                btn_close_login.setVisibility(View.GONE);
                CommonUtils.showToastMsg(getmActivity(), "您已退出登录");
                initViews();
                break;
        }
    }

    public void shotSelectImages() {
        LayoutInflater inflater = LayoutInflater.from(getmActivity());
        myView = (LinearLayout) inflater.inflate(
                R.layout.pop_login_out, null);

        myView.findViewById(R.id.btn_login_out).setOnClickListener(this);
        myView.findViewById(R.id.btn_cancle).setOnClickListener(this);

        myWindow = new PopupWindow(myView,
                RelativeLayout.LayoutParams.FILL_PARENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT, true);
        myWindow.setAnimationStyle(R.style.AnimationPopup);
        myWindow.setOutsideTouchable(true);
        myWindow.setBackgroundDrawable(new BitmapDrawable());
        myWindow.showAtLocation(rlyt_fwgg, Gravity.BOTTOM, 0, 0);
        myWindow.setOnDismissListener(this);
        final Animation animation = (Animation) AnimationUtils.loadAnimation(
                getmActivity(), R.anim.slide_in_from_bottom);
        myView.startAnimation(animation);
        pop_menu_background.setVisibility(View.VISIBLE);
    }

    @Override
    public void onDismiss() {
        pop_menu_background.setVisibility(View.GONE);
    }

    AbstractResponseResult responseResult = new AbstractResponseResult() {

        @Override
        public void executeSuccess(Object object) {
            // TODO
            //  CommonUtils.showToastMsg(getmActivity(), "提交成功！");
        }

        @Override
        public void executeError(String message, int errorCode) {
            // CommonUtils.showToastMsg(getmActivity(), ""+message);
        }

        @Override
        public void HttpError(String message, int errorCode) {
            //CommonUtils.showToastMsg(getmActivity(), ""+message);
        }
    };

    private void loginout() {

        String url = ConstantsServerUrl.getLoginOutUrl();
        RequestParams params = new RequestParams();
        params.put("token", SpUser.getUserToken(getmActivity()));
        CommonResult logResult = new CommonResult(responseResult);
        //SpUser.setUserToken(getmActivity(),null);
        SpUser.clearShare(getmActivity());
        try {
            new HttpRequest(getmActivity()).doPost(logResult, url, params);
        } catch (ArgumentsException e) {
            Log.i("INFO", "1e:" + e);
            e.printStackTrace();
        } catch (RequestException e) {
            Log.i("INFO", "2e:" + e);
            e.printStackTrace();
        }
    }
}
